package com.zkgengkun.interceptor;

import com.zkgengkun.constants.LoginConstants;
import com.zkgengkun.domain.User;
import com.zkgengkun.support.utils.CookieUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author gengshikun
 * @date 2016/11/25
 */
public class LoginInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>登录拦截 ...");

        // 设置当前登录人
        Map<String, Object> cookieMap= CookieUtils.cookies2Map(request);
        String loginToken = (String) cookieMap.get(LoginConstants.CUSTOMER_LOGIN_TOKEN);

        User customer = null;
        boolean haveLogin = false;
        if (StringUtils.isNotBlank(loginToken)) {
            //

            /*customer = JSON.parseObject((String) RedisUtil.get(LoginConstants.CUSTOMER_LOGIN_REDIS_KEY,
                    LoginConstants.CUSTOMER_LOGIN_REDIS_FIELD_HEAD + ":" + loginToken), HbdiyEcCustomer.class);*/
            if (null != customer) {
                haveLogin = true;
            }
        }
        //CustomerLoginSession.setUser(customer);


        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) throws Exception {

    }
}
